document.querySelector(".fanhui").onclick = function () {
    location.href = "./delicacies.html"
}


function getname(key) {
    // 转url地址
    let strData = location.href.split('?')[1];
    let arrData = strData.split('=')
    // console.log(arrData[1]);
    return decodeURI(arrData[1])
}

function getGoodlist() {
    // 假设getname函数已经正确实现了获取URL参数的功能
    const keyword = getname("name");
    // console.log(keyword);


    // 如果keyword存在，构建请求数据，否则直接返回
    ajaxUsePromise02({
        url: `http://localhost:3000/books`,
        method: "get",
        data: `keyword=${encodeURIComponent(keyword)}`
    }).then(res => {
        for (let i = 0; i < res.length; i++) {
            if (keyword == res[i].id) {
                // console.log(res[i]);
                let OcardT = document.querySelector(".card-top")
                let OcardFl = document.querySelector(".card-footl")
                let author = document.querySelector(".author")
                let listshowpic = document.querySelector(".listshow-pic")
                let listshowtit = document.querySelector(".listshowtit")
                let listshowname = document.querySelector(".listshowname")
                let swiperimg = document.querySelector(".swiper-slide")
                // console.log(listshowtit);
                OcardT.innerHTML = res[i].name
                OcardFl.innerHTML = "￥" + res[i].price
                author.innerHTML = res[i].author
                listshowpic.innerHTML = "价格:" + res[i].price + "元"
                listshowtit.innerHTML = "书籍名:" + `《${res[i].name}》`
                listshowname.innerHTML = "作者:" + res[i].author
                let imgsrc = res[i].img
                swiperimg.src = imgsrc;
                document.querySelector(".card-footr").onclick = function () {
                    // console.log(res[i].id);
                    location.href = `./orders.html?category=${res[i].id}`
                }
            }
        }
    });
}

// 用户评论
function getuser() {
    ajaxUsePromise02({
        url: `http://localhost:3000/comments`,
        method: "get",
    }).then(res => {
        // console.log(res);
        comments(res)
    })
}

// 用户评论渲染
function comments(data) {

    const keyword = getname("name");
    // console.log(keyword);


    let htmlstr = "";

    data.forEach(element => {
        
        if (element.bookId == keyword) {
            document.querySelector('.usernum').innerHTML = element.bookId.length
            htmlstr += `
        <div class="box">
            <div class="useri">
                <p class="username">${element.username}</p>
                <p class="usertime">${element.time}</p>
            </div>
                <p class="usercomments">${element.content}</p>
        </div>
        `
            document.querySelector(".UserReviews-three").innerHTML = htmlstr
            
        }

    });
}

window.onload = function () {
    getGoodlist()
    getuser()
}